const connection = require('../app/database');

class SalaryService {
  // 获取基本工资数据
  async getBasicSalary() {
    try {
      const statement = `
        SELECT * FROM salary;
      `;
      const [result] = await connection.execute(statement);
      console.log('[salary.service_getBasicSalary]{success}', result);
      return result;
    } catch(e) {
      console.log('[salary.service_getBasicSalary]{fail}', e);
    }
  }

  // 添加基本工资数据
  async addBasicSalary(args) {
    try {
      let dataArr = [], sqlArr = [], valueArr = [], sql = '', value = '';
      for (let key in args) {
        dataArr.push(args[key]);
        sqlArr.push(key);
        valueArr.push('?')
      }
      sql = sqlArr.join(', ');
      value = valueArr.join(', ');
      const statement = `
        INSERT INTO salary(${sql}) VALUES (${value});
      `;
      const [result] = await connection.execute(statement, dataArr);
      console.log('[salary.service_addBasicSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_addBasicSalary]{fail}', e);
    }
  }

  async editBasicSalary(args) {
    try {
      let dataArr = [], sqlArr = [], sql = '';
      for (let key in args) {
        if (key !== 'id') {
          dataArr.push(args[key]);
          sqlArr.push(`${key} = ?`);
        }
      }
      dataArr.push(args['id']);
      sql = sqlArr.join(', ');
      const statement = `
        UPDATE salary SET ${sql} WHERE id = ?;
      `;
      const [result] = await connection.execute(statement, dataArr);
      console.log('[salary.service_editBasicSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_editBasicSalary]{fail}', e);
    }
  }

  // 删除基本工资数据
  async deleteBasicSalary(args) {
    try {
      const statement = `
        DELETE FROM salary WHERE id = ?;
      `;
      const [result] = await connection.execute(statement, [args['id']]);
      console.log('[salary.service_deleteBasicSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_deleteBasicSalary]{fail}', e);
    }
  }

  // 获取岗位工资数据
  async getWorkTypeSalary() {
    try {
      const statement = `
        SELECT * FROM workType;
      `;
      const [result] = await connection.execute(statement);
      console.log('[salary.service_deleteBasicSalary]{success}', result);
      return result;
    } catch(e) {
      console.log('[salary.service_deleteBasicSalary]{fail}', e);
    }
  }

  // 新增岗位工资数据
  async addWorkTypeSalary(args) {
    try {
      const statement = `
        INSERT INTO workType (work, workSalary) VALUES (?, ?);
      `;
      const [result] = await connection.execute(statement, [args['work'], args['workSalary']]);
      console.log('[salary.service_addWorkTypeSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_addWorkTypeSalary]{fail}', e);
    }
  }

  // 编辑岗位工资数据
  async editWorkTypeSalary(args) {
    try {
      const statement = `
        UPDATE workType SET work = ?, workSalary = ? WHERE id = ?;
      `;
      const [result] = await connection.execute(statement, [args['work'], args['workSalary'], args['id']]);
      console.log('[salary.service_editWorkTypeSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_editWorkTypeSalary]{fail}', e);
    }
  }

  // 删除岗位工资数据
  async deleteWorkTypeSalary(args) {
    try {
      const statement = `
        DELETE FROM workType WHERE id = ?;
      `;
      const [result] = await connection.execute(statement, [args['id']]);
      console.log('[salary.service_deleteWorkTypeSalary]{success}', result);
      return true;
    } catch(e) {
      console.log('[salary.service_deleteWorkTypeSalary]{fail}', e);
    }
  }
}

module.exports = new SalaryService();